import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

/// 基础动画
class BaseAnimation extends StatefulWidget {
  BaseAnimation({this.text}) : super();
  final String? text;

  @override
  State<StatefulWidget> createState() {
    return _AnimationPageState();
  }
}

class _AnimationPageState extends State<BaseAnimation>
    with SingleTickerProviderStateMixin {
  Animation<double>? animation;
  AnimationController? _controller;

  @override
  void initState() {
    super.initState();
    _controller =
        new AnimationController(duration: Duration(seconds: 3), vsync: this);
    //使用弹性曲线
    animation = CurvedAnimation(parent: _controller!, curve: Curves.bounceIn);
    //图片宽高从0变到300
    animation = new Tween(begin: 0.0, end: 250.0).animate(_controller!);
    animation?.addStatusListener((status) {
      ///dismissed	动画在起始点停止
      ///forward	动画正在正向执行
      ///reverse	动画正在反向执行
      ///completed	动画在终点停止
      if (status == AnimationStatus.completed) {
        //动画执行结束时反向执行动画
        _controller?.reverse();
      } else if (status == AnimationStatus.dismissed) {
        //动画恢复到初始状态时执行动画（正向）
        _controller?.forward();
      }
    });
    animation?.addListener(() {
      //监听刷新
      if (!_controller!.isDismissed) {
        setState(() {});
      }
    });
    _controller?.forward();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.text ?? ""),
      ),
      body: Align(
        alignment: Alignment.center,
        // child: Image.asset('images/iron_man.jpg', width: animation.value, height: animation.value),
        child: Image.asset('assets/images/iron_man.jpg',
            width: animation?.value, height: animation?.value),
      ),
    );
  }

  @override
  void dispose() {
    //放在这里做消失
    _controller?.dispose();
    super.dispose();
  }
}
